package sandau.self_improvement_wall.controller;

import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import sandau.self_improvement_wall.domain.ImproveInfo;
import sandau.self_improvement_wall.domain.ImproveInfoExample;
import sandau.self_improvement_wall.mapper.ImproveInfoMapper;
import sandau.self_improvement_wall.vo.BasicMsg;
import sandau.self_improvement_wall.vo.BasicMsgWithData;
import sandau.self_improvement_wall.vo.SelfImprovementVo;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api")
public class SelfimprovementController {
    @Resource
    private ImproveInfoMapper improveInfoMapper;
    @GetMapping("/selfimprovementlist")
    public BasicMsgWithData<SelfImprovementVo> queryImprovement(){
        BasicMsgWithData<SelfImprovementVo> msgWithData = new BasicMsgWithData<>();
        ImproveInfo improveInfo = improveInfoMapper.selectByPrimaryKey(1L);
        SelfImprovementVo selfImprovementVo = new SelfImprovementVo();
        selfImprovementVo.setGoal(improveInfo.getInfo());
        selfImprovementVo.setReward(improveInfo.getReward());
        selfImprovementVo.setPunishment(improveInfo.getPunish());
        msgWithData.setData(selfImprovementVo);
        return msgWithData;
    }
    @GetMapping("/selfimprovementlist/{userid}")
    public BasicMsgWithData<List<SelfImprovementVo>> queryImproveList(@PathVariable long userid){
        BasicMsgWithData<List<SelfImprovementVo>> msgWithData = new BasicMsgWithData<>();
        //       ImproveInfo improveInfo = improveInfoMapper.selectByPrimaryKey(1L);
        //     SelfImprovementVo selfImprovementVo = new SelfImprovementVo();
        //     selfImprovementVo.setInfo(improveInfo.getInfo());
        //       selfImprovementVo.setReward(improveInfo.getReward());
        //      selfImprovementVo.setPunish(improveInfo.getPunish());
        //  msgWithData.setData(selfImprovementVo);
        ImproveInfoExample example = new ImproveInfoExample();
        ImproveInfoExample.Criteria criteria = example.createCriteria();
        criteria.andUseridEqualTo(userid);
        List<ImproveInfo> improveInfos = improveInfoMapper.selectByExample(example);
        List<SelfImprovementVo> selfImprovementVos = new ArrayList<>();
        for (ImproveInfo i : improveInfos) {
            SelfImprovementVo selfImprovementVo = new SelfImprovementVo();
            selfImprovementVo.setGoal(i.getInfo());
            selfImprovementVo.setReward(i.getReward());
            selfImprovementVo.setPunishment(i.getPunish());
            selfImprovementVos.add(selfImprovementVo);
        }
        msgWithData.setData(selfImprovementVos);
        return msgWithData;
    }

    @PostMapping("/selfimprovementlist")
    public BasicMsg submit(@RequestBody SelfImprovementVo selfImprovementVo){
        BasicMsg msg = new BasicMsg(0,"");
        ImproveInfo improveInfo = new ImproveInfo();
        improveInfo.setInfo(selfImprovementVo.getGoal());
        improveInfo.setPunish(selfImprovementVo.getPunishment());
        improveInfo.setReward(selfImprovementVo.getReward());
        improveInfo.setUserid(1L);
        improveInfoMapper.insert(improveInfo);
        return msg;
    }
}


